package org.example.executor;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Yyming
 * @version 10.0
 * Created by Yyming on 2020/11/10
 */
public class MainTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ReentrantLock lock = new ReentrantLock(true);
        Condition condition = lock.newCondition();

        Condition condition1 = lock.newCondition();

        CountDownLatch downLatch = new CountDownLatch(1);

        new Thread(() -> {
            lock.lock();
            try {
                while (true) {
                    condition.await();
                    downLatch.countDown();
                    System.out.println(1);
                    condition.signal();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.lock();
            }
        }).start();

        new Thread(() -> {

            lock.lock();
            try {
                condition.signal();
                while (true) {
                    condition.await();
                    System.out.println(2);
                    condition.signal();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
    }



}
